var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");
var billid_id = sessionStorage.getItem("billid_id");

$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
	} else {
		var pageNumber = "1";
		var pageSize = "10";
		var selectinfo = "";
		var reque = function() {
			return {
				pageSize: pageSize,
				pageNumber: pageNumber,
				billid: billid_id,
				selectinfo: selectinfo
			}
		}
		rq1 = reque();
		var req = function() {
			return {
				requestString: "",
				userId: userId,
				fullname: fullname
			}
		}
		requestObj1 = req();
		requestObj1.requestString = JSON.stringify(rq1);
		var req3 = JSON.stringify(requestObj1);
		ajax_data(req3)
	}

});

function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrgainzeSettleInfo',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "10";
	var selectinfo = trim($("#text_sousuo").val());

	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			billid: billid_id,
			selectinfo: selectinfo
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectOrgainzeSettleInfo',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
		console.log(bb);
		var bb = JSON.parse(bb);
		var tableStr = "";
		var len = bb.length;
		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				0 未支付 1 已支付 2 已取消
				//				3 已退款 4 缴费失败， 已退款
				if(bb[i].auditusername == undefined) {
					bb[i].auditusername = '';
				}
				if(bb[i].summary == undefined) {
					bb[i].summary = '';
				}
				if(bb[i].billdate == undefined) {
					bb[i].billdate = '';
				}
				if(bb[i].paymentamt == undefined) {
					bb[i].paymentamt = '';
				}
				if(bb[i].chargeway == 0) {
					bb[i].chargeway = "app";
				} else {
					bb[i].chargeway = "物业后台";
				}
				if(bb[i].settleway == 1) {
					bb[i].settleway = "系统自动结算";
				} else if(bb[i].settleway == 2) {
					bb[i].settleway = "人工结算";
				}
				switch(bb[i].isaudit) {
					case 0:
						bb[i].isaudit = "未审核";
						break;
					case 1:
						bb[i].isaudit = "审核通过";
						break;
					case 2:
						bb[i].isaudit = "审核不通过";
						break;

				}
				if(bb[i].apipayid == undefined) {
					bb[i].apipayid = ""
				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb[i].merchantname + '</th>' +
					'<th>' + bb[i].areaname + '</th>' +
					'<th>' + bb[i].weixinsettleqty + '</th>' +
					'<th>' + bb[i].weixinsettleamt + '</th>' +
					'<th>' + bb[i].alisettleqty + '</th>' +
					'<th>' + bb[i].alisettleamt + '</th>' +
					'<th >' + bb[i].settleqty + '</th>' +
					'<th >' + bb[i].settleamt + '</th>' +

					//
					//					'<th  style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-edit"></span></th>' +
					//					'<th  style="color:red;cursor: pointer" ;><span class="glyphicon glyphicon-time"></span></th>' +

					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}

		//点击详情
		$("#tbbb tr th:nth-child(17)").click(function() {

			//			s_djrq  s_shsj  s_shr  s_wxjsts  s_wxjsje  s_zfbjsts s_zfbjsje s_zy
			$('#myModal').modal('show')
			$("#s_djrq").html($(this).siblings().eq(8).html());
			$("#s_shsj").html($(this).siblings().eq(9).html());
			$("#s_shr").html($(this).siblings().eq(10).html());
			$("#s_wxjsts").html($(this).siblings().eq(11).html());
			$("#s_wxjsje").html($(this).siblings().eq(12).html());
			$("#s_zfbjsts").html($(this).siblings().eq(13).html());
			$("#s_zfbjsje").html($(this).siblings().eq(14).html());
			$("#s_zy").html($(this).siblings().eq(15).html());
		});
		$("#tbbb tr th:nth-child(18)").click(function() {
			var id = $(this).siblings().eq(1).html();
			sessionStorage.setItem("billid_id", id);
			//			window.location.href="zhanwei_history.html";
		})

	}
}

//模糊查询
$('#search_chaxun').click(function() {
	
	var pageNumber = "1";
	var pageSize = "10";
	
	var selectinfo = trim($("#text_sousuo").val());
	//	var userId = "";
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			billid: billid_id,
			selectinfo: selectinfo
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	ajax_data(req3)

})